package com.gdk.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gdk.utils.CommonUtils;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;


/**
 * controller
 * */

@Controller
public class UploadController {
	
	 @ResponseBody
	 @RequestMapping("/uploadToQiNiu.do")  
	 public Map<String,Object> upload(HttpServletRequest request) throws Exception {  
	        // 判断enctype属性是否为multipart/form-data  
	        boolean isMultipart = ServletFileUpload.isMultipartContent(request);  
	        if (!isMultipart)  
	            throw new IllegalArgumentException(  
	                    "上传内容不是有效的multipart/form-data类型.");  
	  
	        // Create a factory for disk-based file items  
	        DiskFileItemFactory factory = new DiskFileItemFactory();  
	  
	        // Create a new file upload handler  
	        ServletFileUpload upload = new ServletFileUpload(factory);  
	  
	        // Parse the request  
	        List<?> items = upload.parseRequest(request);  
	  
	        //返回结果    200成功
	        Map<String,Object> resultMap = new HashMap<String,Object>();
	        int status = 0;
	        
	        Iterator iter = items.iterator();  
	        while (iter.hasNext()) {  
	            FileItem item = (FileItem) iter.next();  
	  
	            if (item.isFormField()) {  
	                // 如果是普通表单字段  
	                String name = item.getFieldName();  
	                String value = item.getString();  
	                // ...  
	            } else {  
	                // 如果是文件字段  
	                String fieldName = item.getFieldName();  
	                String fileName = item.getName();  
	                String contentType = item.getContentType();  
	                boolean isInMemory = item.isInMemory();  
	                long sizeInBytes = item.getSize();  
	                
	                
	                
	    
	                
	                // ...  
	                // 上传到远程服务器  
	                HashMap<String, FileItem> files = new HashMap<String, FileItem>();  
	                files.put(fileName, item);  
	                Map<String, Object> uploadToQiNiuYun = uploadToQiNiuYun(files); 
	                status = (int) uploadToQiNiuYun.get("status");
	                if(status == 200) {
	                	System.out.println("上传成功！                              文件链接地址   :    "+ uploadToQiNiuYun.get("file_url"));
	                	resultMap.put("code", 200);
	                	resultMap.put("file_url", uploadToQiNiuYun.get("file_url"));
	                }else if(status == 400) {
	                	System.out.println("上传失败!");
	                	resultMap.put("code", 400);
	                }
	            }  
	        }  
	        return resultMap;  
	    }
 
 
	private Map<String,Object> uploadToQiNiuYun(HashMap<String, FileItem> files) throws IOException {
		//构造一个带指定Zone对象的配置类
		Configuration cfg = new Configuration(Zone.zone0());
		//...其他参数参考类注释
//		华东	Zone.zone0()
//		华北	Zone.zone1()
//		华南	Zone.zone2()
//		北美	Zone.zoneNa0()
 
		UploadManager uploadManager = new UploadManager(cfg);
		//...生成上传凭证，然后准备上传
		//String accessKey = "gHUr0wk1ruvjfaxyxKgg0G_-5poGxVpeXT_A9zqL";//这里请替换成自己的AK
		//String secretKey = "Zk1hitP06ywSBDCEtoEsphrtHzl-XUtdM3DUCqMM";//这里请替换成自己的SK
	
		
		//String bucket = "cx_zhizaido_com";//这里请替换成自己的bucket--空间名
		String accessKey = "gHUr0wk1ruvjfaxyxKgg0G_-5poGxVpeXT_A9zqL";//这里请替换成自己的AK
		String secretKey = "Zk1hitP06ywSBDCEtoEsphrtHzl-XUtdM3DUCqMM";//这里请替换成自己的SK
		
		String bucket = "farm-yt";//这里请替换成自己的bucket--空间名
		
		// String accessKey = "gHUr0wk1ruvjfaxyxKgg0G_-5poGxVpeXT_A9zqL";
	     //   String secretKey = "Zk1hitP06ywSBDCEtoEsphrtHzl-XUtdM3DUCqMM";
	        // 要上传的空间名--
	     //   String bucket = "dingdao";
		
		
		//默认不指定key的情况下，以文件内容的hash值作为文件名
		String key =  null;
		//返回结果
		Map<String,Object> resultMap = new HashMap<String,Object>();
		
		 Iterator iter = files.entrySet().iterator();  
         int i=0;  
         while (iter.hasNext()) {  
             i++;  
             Map.Entry entry = (Map.Entry) iter.next();  
             String fileName = (String) entry.getKey();  
             
             String imgType = fileName.substring(fileName.lastIndexOf("."), fileName.length());
             
             
             //key = CommonUtils.getUUID()+"_"+fileName;
             key = CommonUtils.getUUID()+imgType;             
             FileItem val = (FileItem) entry.getValue();
             InputStream inputStream=val.getInputStream();
             ByteArrayOutputStream swapStream = new ByteArrayOutputStream(); 
             byte[] buff = new byte[600]; //buff用于存放循环读取的临时数据 
             int rc = 0; 
             while ((rc = inputStream.read(buff, 0, 100)) > 0) { 
             swapStream.write(buff, 0, rc); 
             } 
             byte[] uploadBytes  = swapStream.toByteArray(); //uploadBytes 为转换之后的结果 
             Auth auth = Auth.create(accessKey, secretKey);
			String upToken = auth.uploadToken(bucket);
			try {
			    Response response = uploadManager.put(uploadBytes, key, upToken);
			 //   System.out.println("result  : "+response);
			    //解析上传成功的结果
			    DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
			 //   System.out.println("上传文件名  : " + putRet.key);
			 //   System.out.println("Hash值 : " + putRet.hash);
			    resultMap.put("status",200);
			    resultMap.put("file_url", "https://nc.cdn.gengduoke.com/"+putRet.key);
			    resultMap.put("fileName", putRet.key);
			    
			} catch (QiniuException ex) {
			    Response r = ex.response;
			    System.err.println(r.toString());
			    try {
			        System.err.println(r.bodyString());
			    } catch (QiniuException ex2) {
			        //ignore
			    }
			    resultMap.put("status",400);
			}
         }
         return resultMap;
		
	}  
	
	
	

	
}